/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2019 HUST Dian Group
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Original Author: Pengyu Liu <eicliupengyu@gmail.com>
 */

#include "robo-team-helper.h"
#include "ns3/variable-speed-mobility.h"
#include "ns3/function-mobility.h"
#include "ns3/const-location-mobility.h"
#include "ns3/robo-common.h"
#include "ns3/robo-judge.h"

namespace ns3 {
RoboTeamHelper::RoboTeamHelper ()
{
}

RoboTeamHelper::~RoboTeamHelper ()
{
}

void
RoboTeamHelper::Init (uint8_t teamId, FVector location, FAngle rotation, RoboTeamConfig cfgs)
{
  m_teamId = teamId;
  m_initLocation = location;
  m_initRotation = rotation;
  RoboJudge::Get ()->Init ();
  m_nodes.Create (cfgs.size ());
  uint32_t idx = 0;
  bool invincible = false;
  uint8_t towerUid = 255;
  for (auto cfg : cfgs)
    {
      Ptr<RoboBase> robo = CreateObject<RoboBase> ();
      m_robo.push_back (robo);
      m_name2uid[cfg.name] = robo->m_uid;
      m_uid2idx[robo->m_uid] = idx;
      robo->m_team = teamId;
      robo->m_roboType = cfg.type;
      robo->m_name = cfg.name;
      robo->m_life = gRoboMaxLife[cfg.type][0];
      m_nodes.Get (idx++)->AggregateObject (robo);
      robo->m_globalLocation = ConvCoordinate (location, rotation, cfg.location);
      robo->m_globalRotation = cfg.rotation + rotation;
      robo->m_collisionType = Collision_Type_Polygon;
      robo->AddBoundaryPoint (gBoundaryPoints[cfg.type]);
      robo->m_collisionMask = gRoboCollisionMask[cfg.type];
      robo->m_selfMask = gRoboMask[cfg.type];

      for (int i = 0; i < MAX_WEAPON_ID; ++i)
        {
          robo->m_weapon[i].m_ammoNumber = gWeaponInitAmmoNum[cfg.type][i];
        }
      switch (cfg.type)
        {
        case RoboTypeUVA:
          {
            robo->m_invincible = true;
          }
        case RoboTypeHero:
        case RoboTypeInfantry:
        case RoboTypeEngineer:
          {
            Ptr<VariableSpeedMobility> mob = CreateObject<VariableSpeedMobility> ();
            robo->SetMobility (mob);
            mob->SetActor (robo);
            mob->SetMaxSpeed (gRoboMaxSpeed[cfg.type]);
            break;
          }
        case RoboTypeSentinel:
          {
            invincible = true;
            Ptr<FunctionMobility> mob = CreateObject<FunctionMobility> ();
            robo->SetMobility (mob);
            mob->SetActor (robo);
            mob->SetFunction (MakeBoundCallback (&DefaultLocFunc, m_initLocation.m_x,
                                                 m_initLocation.m_y, m_initRotation.m_phi));
            break;
          }
        case RoboTypeTower:
          {
            towerUid = robo->m_uid;
            Ptr<ConstLocationMobility> mob = CreateObject<ConstLocationMobility> ();
            robo->SetMobility (mob);
            mob->SetActor (robo);
            break;
          }

        default:
          break;
        }
      RoboJudge::Get ()->AddRobo (robo);
    }
  m_supply = CreateObject<RoboSupply> ();
  m_supply->m_globalLocation = ConvCoordinate (location, rotation, FVector (0, 0));
  m_supply->m_globalRotation = rotation;
  m_supply->m_collisionType = Collision_Type_Polygon;
  m_supply->AddBoundaryPoint (gSupplyBoundary);
  m_supply->m_collisionMask = gBuildingCollisionMask;
  m_supply->m_selfMask = SelfMaskBuilding;
  m_supply->m_team = teamId;
  m_supply->m_incLife = true;
  RoboJudge::Get ()->AddBuilding (m_supply);
  if (towerUid!=255 && invincible)
    {
      GetRoboOf (towerUid)->m_invincible = true;
    }
}

Ptr<Node>
RoboTeamHelper::GetNodeOf (std::string name)
{
  return m_nodes.Get (m_uid2idx[m_name2uid[name]]);
}

Ptr<Node>
RoboTeamHelper::GetNodeOf (uint8_t uid)
{
  return m_nodes.Get (m_uid2idx[uid]);
}

Ptr<RoboBase>
RoboTeamHelper::GetRoboOf (std::string name)
{
  return m_robo[m_uid2idx[m_name2uid[name]]];
}

Ptr<RoboBase>
RoboTeamHelper::GetRoboOf (uint8_t uid)
{
  return m_robo[m_uid2idx[uid]];
}

NodeContainer &
RoboTeamHelper::GetNodes ()
{
  return m_nodes;
}

uint8_t
RoboTeamHelper::GetTeamId ()
{
  return m_teamId;
}
} // namespace ns3